package com.thinkfit.jy.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.common.lib.bean.VersionBean;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.manager.DataManager;
import com.jaygoo.widget.OnRangeChangedListener;
import com.jaygoo.widget.RangeSeekBar;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.VersionActivity;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.CountDownViewUtils;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.DialogViewUtils;
import com.thinkfit.jy.utils.EquipmentInfoUtils;
import com.thinkfit.jy.utils.LogUtils;
import com.thinkfit.jy.utils.Utils;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;
import java.util.ArrayList;

public class CustomSportsPopupWindow extends PopupWindow implements View.OnClickListener , PopupWindow.OnDismissListener {

    private Context mContext;
    int type;
    private View view;
    TextView tvTitle,tvNum,tvOk;
    ImageView ivReduce,ivAdd,ivBg;
    RangeSeekBar rsb;
    float mVolume;

    int mProgress,mMaxItem;

    public CustomSportsPopupWindow(Context context,int type){
        mContext = context;
        this.type = type;
        view = LayoutInflater.from(context).inflate(R.layout.item_custom_sports, null);
        tvTitle = view.findViewById(R.id.tv_title);
        tvNum = view.findViewById(R.id.tv_num);
        tvOk = view.findViewById(R.id.tv_ok);
        ivReduce = view.findViewById(R.id.iv_reduce);
        ivAdd = view.findViewById(R.id.iv_add);
        ivBg = view.findViewById(R.id.iv_bg);
        rsb = view.findViewById(R.id.rsb);

        if(type == 2){
            tvTitle.setText(mContext.getString(R.string.app_customized)+mContext.getString(R.string.app_time));
            mMaxItem = 180;
            rsb.setRange(0,180);
        }else if(type == 3){
            tvTitle.setText(mContext.getString(R.string.app_customized)+mContext.getString(R.string.app_use_up));
            mMaxItem = 2000;
            rsb.setRange(0,2000);
        }else {
            tvTitle.setText(mContext.getString(R.string.app_customized)+mContext.getString(R.string.app_distance_1));
            mMaxItem = 500;
            rsb.setRange(0,500);
        }


//        tvBrigthnessNum.setText(brigthnessProgress+"%");
        rsb.setOnRangeChangedListener(new OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
                mProgress = (int) (leftValue + 0.0001);
                if(type == 1){
                    tvNum.setText((mProgress/10f)+"");
                }else {
                    tvNum.setText(mProgress+"");
                }

            }

            @Override
            public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }

            @Override
            public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }
        });


        tvOk.setOnClickListener(this);
        ivReduce.setOnClickListener(this);
        ivAdd.setOnClickListener(this);
        ivBg.setOnClickListener(this);

        this.setContentView(view);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();

        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#4D000000"));
//        ColorDrawable dw = new ColorDrawable(Color.parseColor("#ffffff"));
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(this);

    }

    public void show(Activity context) {
        showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_ok:
                initTvItem();
                break;
            case R.id.iv_reduce:
                reduceNum();
                break;
            case R.id.iv_add:
                addNum();
                break;
            case R.id.iv_bg:
                dismiss();
                break;

        }
    }
    ArrayList<Float> setTarget = new ArrayList<>();
    public void initTvItem(){
        setTarget.clear();
        String textById = tvNum.getText().toString().trim();
        setTarget.add((type+0f));
        setTarget.add(Float.valueOf(textById));
        DataManager.Companion.getInstance().saveSetTarget(setTarget);

        if(DataManager.Companion.getInstance().isLogin()){
            if(DeviceTypeUtils.getDeviceType() == 2){
                if(DeviceTypeUtils.connectDeviceType() == 0){
                    if(DeviceTypeUtils.getInstance().isTreadmill()){
                        BluetoothOrder.getInstance().startTreadmill();
                    }else {
                        BluetoothOrder.getInstance().startPlay();
                    }
                }else {
                    SerialPortManager.getInstance().startRun();
                }
            }else {
                CountDownViewUtils.getInstance().initView(mContext,3);
            }
//                    CountDownViewUtils.getInstance().initView(mContext,3);
        }else {
            DialogViewUtils.getInstance().initView(mContext,false,DialogViewUtils.TOURIST_PATTERN);
        }
    }

    public void reduceNum(){
        String textById = tvNum.getText().toString().trim();

        Float aFloat = Float.valueOf(textById);
        if(type == 2){
            mVolume = aFloat - 1;
        }else if(type == 3){
            mVolume = aFloat - 1;
        }else {
            mVolume = aFloat*10f - 1f;
        }

        if(mVolume <= 0){
            mVolume = 0;
        }

        if(type == 1){
            tvNum.setText(Utils.getNewFloat(mVolume/10f));
        }else {
            tvNum.setText(Math.round(mVolume)+"");
        }
        rsb.setProgress(mVolume);

    }

    public void addNum(){
        String textById = tvNum.getText().toString().trim();

        Float aFloat = Float.valueOf(textById);
        if(type == 2){
            mVolume = aFloat + 1;
        }else if(type == 3){
            mVolume = aFloat + 1;
        }else {
            mVolume = aFloat*10f + 1f;
        }
        if(mVolume >= mMaxItem){
            mVolume = mMaxItem;
        }

        if(type == 1){
            tvNum.setText(Utils.getNewFloat(mVolume/10f));
        }else {
            tvNum.setText(Math.round(mVolume)+"");
        }
        rsb.setProgress(mVolume);
    }

    @Override
    public void onDismiss() {
          dismiss();
    }
}
